﻿using BCGPSCommon;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver.GeoJsonObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCMongoEntity.TMS.Fence
{
    /// <summary>
    /// 电子围栏
    /// </summary>
    [BsonIgnoreExtraElements]
    public class FenceMongoEntity
    {
        /// <summary>
        /// Id
        /// </summary>
        public string Id { set; get; }


        /// <summary>
        /// 围栏名称
        /// </summary>
        public string FenceName { get; set; }
        /// <summary>
        /// 围栏编号
        /// </summary>
        public int FenceNumber { get; set; }
        /// <summary>
        /// 控制Id
        /// </summary>
        public long CtrlCompanyId { get; set; }
        /// <summary>
        /// 控制公司名称
        /// </summary>
        public string CtrlCompanyName { get; set; }
        /// <summary>
        /// 劳务公司Id
        /// </summary>
        public long WorkCompanyId { get; set; }
        /// <summary>
        /// 劳务公司名称
        /// </summary>
        public string WorkCompanyName { get; set; }
        /// <summary>
        /// 省/州编码
        /// </summary>
        public string ProvinceCode { get; set; }
        /// <summary>
        /// 省/州名称
        /// </summary>
        public string ProvinceName { get; set; }
        /// <summary>
        /// 市编码
        /// </summary>
        public string  CityCode { get; set; }
        /// <summary>
        /// 市名称
        /// </summary>
        public string CityName { get; set; }
        /// <summary>
        /// 县编码
        /// </summary>
        public string  CountyCode { get; set; }
        /// <summary>
        /// 县名称
        /// </summary>
        public string CountyName { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 审核时间
        /// </summary>
        public DateTime? ApproveTime { get; set; }
        /// <summary>
        /// 审核人Id
        /// </summary>
        public long? ApproverId { get; set; }
        /// <summary>
        /// 审核人名称
        /// </summary>
        public string Approver { get; set; }
        /// <summary>
        /// 修改人Id
        /// </summary>
        public long EditorId { get; set; }
        /// <summary>
        /// 修改人名称
        /// </summary>
        public string Editor { get; set; }
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime EditTime { get; set; }
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnable { get; set; }
        /// <summary>
        /// 电子围栏报警类型
        /// </summary>
        public int FenceAlarmType { get; set; } 
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 多边形范围
        /// </summary>          
        public List<Map2DCoordinate> CoordinateList
        {
            get
            {
                if (Polygon != null && Polygon.Coordinates != null && Polygon.Coordinates.Exterior != null && Polygon.Coordinates.Exterior.Positions != null && Polygon.Coordinates.Exterior.Positions.Count > 0)
                {
                    List<Map2DCoordinate> list = new List<Map2DCoordinate>();
                    foreach (var item in Polygon.Coordinates.Exterior.Positions)
                    {
                        list.Add(new Map2DCoordinate(item.Latitude, item.Longitude));
                    }
                    return list;
                }
                return null;
            }
            set
            {
                if (value.Count < 4)
                    throw new ArgumentException("围栏不能小于4个点!");

                if (value[0].lat != value[value.Count - 1].lat
                    || value[0].lng != value[value.Count - 1].lng)
                    throw new ArgumentException("围栏第一个点必须和最后一个点重合!");

                var list = new List<GeoJson2DGeographicCoordinates>();
                foreach (var item in value)
                {
                    list.Add(new GeoJson2DGeographicCoordinates(item.lng, item.lat));
                }
                try
                {
                    SetPolygon(list);
                }
                catch (System.Exception ex)
                {
                    throw new ArgumentException(ex.Message);
                }

            }
        }


        /// <summary>
        /// 多边形范围
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public GeoJsonPolygon<GeoJson2DGeographicCoordinates> Polygon { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="coordinatesList"></param>
        public void SetPolygon(List<GeoJson2DGeographicCoordinates> coordinatesList)
        {
            Polygon = new GeoJsonPolygon<GeoJson2DGeographicCoordinates>(
                                new GeoJsonPolygonCoordinates<GeoJson2DGeographicCoordinates>(
                                    new GeoJsonLinearRingCoordinates<GeoJson2DGeographicCoordinates>(
                                        coordinatesList)));
        }

    }
}
